package com.lz.football.module.wineBar.view
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.WinebarEvent;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.wineBar.IWineAwardPanel;
	import com.lz.football.module.wineBar.item.WineItem;
	
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.core.TitleWindow;

	/**
	 *奖励面板 
	 * @author lake
	 * 
	 */	
	public class AwardPanel extends TitleWindow implements IWineAwardPanel
	{
		/**
		 *确认按钮 
		 */		
		private var sureBtn 	:SimpleButton;
		/**
		 *描述 
		 */		
		private var txtDesc	:TextField;
		/**
		 *无额外奖奖励 
		 */		
		private var txtNoAward	:TextField;
		/**
		 *item 列表 
		 */		
		private var itemList	:Array = [];
		/**
		 *选择的酒目 
		 */		
		private var selectItem :WineItem;
		/**
		 *数据 
		 */		
		private var _data		:*;
		/**
		 *奖励类型
		 * 	1-	点券购买额外奖励
			2-	额外赠送经验
			3-	没有额外经验 
		 */		
		private var awardType 	:int = 1;
		
		public function AwardPanel()
		{
			super();
		}
		/**
		 *设置获取数据 
		 * @param value
		 * 
		 */		
		public function set data(value :*):void
		{
			_data = value;
			awardType = _data.type;
			var exp :int = _data.exp;
			var i:int = 0;
			var wineItem :WineItem;
			var info :String = "";
			switch(awardType)
			{
				case 1://点券购买额外奖励
				{
					info = "恭喜你本次泡吧你已获得"+exp+"点经验，酒吧老板高兴拿出自己珍藏多年的酒半价出售给你，是否购买。";
					break;
				}
				case 2://额外赠送经验
				{
					info = "恭喜你本次泡吧你已获得"+exp+"点经验，由于你是本店第8888个顾客，本店额外赠送你一瓶杰克丹尼。";
					//奖经验
					for(i = 0;i < 4;i++)
					{
						wineItem = itemList[i];
						wineItem.selectBtn.visible = false;
						wineItem.txtValue.text = "0";
						_symbolSkin["item_" + i]["visible"] = false;
					}
					_symbolSkin["item_" + 1]["visible"] = true;
					_symbolSkin["item_" + 1].x = 344;
					_symbolSkin["item_" + 1].y = 143;
					break;
				}
				case 3://额外赠送经验
				{
					info = "恭喜你本次泡吧你已获得"+exp+"点经验，欢迎你下次继续光临。";
					for(i = 0;i < 4;i++)
					{
						_symbolSkin["item_" + i]["visible"] = false;
					}
					txtNoAward.visible = true;
					break;
				}
			}
			txtDesc.htmlText = info;
		}
		public function get data():*
		{
			return _data;
		}
		/**
		 *初始化 
		 * 
		 */		
		public function initUI(view :Object = null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.winebar.AwardPanelSkin");
			this.addChild(_symbolSkin);
			sureBtn = _symbolSkin.sureBtn;
			txtDesc = _symbolSkin.txtDesc;
			txtNoAward = _symbolSkin.txtNoAward;
			txtNoAward.visible = false;
			//背景框
			windowBg = _symbolSkin.windowBg;
			//设置可拖动
			dragEnabled = true;
			//初始化宽高
			width 	= _symbolSkin.width;
			height	= _symbolSkin.height;
			//初始化item
			var wineItem :WineItem;
			var prices :Array = [200,200,200,600];
			var tips  :Array = [];
			var info :String = "<p align='left'><font color='#0033CC'>轩尼诗</font>\n\n可获得100W铜钱</p>";
			tips.push(info);
			info = "<p align='left'><font color='#0033CC'>杰克丹尼</font>\n\n可获得10W经验</p>";
			tips.push(info);
			info = "<p align='left'><font color='#0033CC'>威士忌</font>\n\n可获得10W声望</p>";
			tips.push(info);
			info = "<p align='left'><font color='#FFFF33'>拉菲</font>\n\n可获得100W铜钱，10W经验，10W声望</p>";
			tips.push(info);
			var i :int = 0;
			for(i = 0;i < 4;i++)
			{
				wineItem = new WineItem();
				wineItem.initUI(_symbolSkin["item_" + i]);
				wineItem.price = prices[i];
				wineItem.type = i + 1;
				itemList.push(wineItem);
				wineItem.addEventListener("selectWine",selectWineHandler);
				DisplayMouseEventManager.instance.addTipsDisplay(wineItem.symbolSkin,tips[i]);
			}
			selectItem = itemList[0];
			selectItem.isSelect = true;
			//添加事件
			addEvtListener();
		}
		/**
		 *注册事件 
		 * 
		 */		
		public function addEvtListener():void
		{
			sureBtn.addEventListener(MouseEvent.CLICK,sureHandler);
		}
		/**
		 *选择酒 
		 * @param e
		 * 
		 */		
		private function selectWineHandler(e :Event):void
		{
			if(selectItem == e.target)//选择同一种酒
			{
				if(selectItem.isSelect)
				{
					selectItem.isSelect = false;
				}
				else
				{
					selectItem.isSelect = true;
				}
				return;
			}
			if(selectItem)
			{
				selectItem.isSelect = false;
			}
			selectItem = e.target as WineItem;
			if(selectItem)
			{
				selectItem.isSelect = true;
			}
		}
		/**
		 *确认 
		 * @param e
		 * 
		 */		
		private function sureHandler(e :MouseEvent):void
		{
			sureBtn.mouseEnabled = false;
			if(awardType != 1 || selectItem.isSelect == false)
			{
				if(awardType == 2)
				{
					AppEventDispatcher.getInstance().dispatchEvent(new WinebarEvent(WinebarEvent.GET_AWARD,5));
				}
				else
				{
					AppEventDispatcher.getInstance().dispatchEvent(new WinebarEvent(WinebarEvent.GET_AWARD,6));
				}
				this.dispatchEvent(new Event("Close_AwardPanel"));
				super.onCloseHandler(null);
				return;
			}
			var totalNum :int = selectItem.price;;
			var info :String = "确定花费" + totalNum + "点券购买额外的奖励吗？";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,buyAlertHandler);
		}
		/**
		 * 购买 确认
		 * @param type
		 * 
		 */		
		private function buyAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				var type	 :int = selectItem.type;
				AppEventDispatcher.getInstance().dispatchEvent(new WinebarEvent(WinebarEvent.GET_AWARD,type));
				this.dispatchEvent(new Event("Close_AwardPanel"));
				super.onCloseHandler(null);
			}
		}
		/**
		 *购买返回 
		 * 
		 */		
		public function buyRes(obj :Object):void
		{
			if(obj.retcode == 0)
			{
				this.dispatchEvent(new Event("Close_AwardPanel"));
				super.onCloseHandler(null);
			}
			else
			{
				sureBtn.mouseEnabled = true;
			}
		}
		/**
		 *删除事件 
		 * 
		 */		
		public function removeEvtListener():void
		{
			sureBtn.removeEventListener(MouseEvent.CLICK,sureHandler);
		}
		/**
		 *销毁 
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			for each(var wineItem :WineItem in itemList)
			{
				wineItem.removeEventListener("selectWine",selectWineHandler);
				DisplayMouseEventManager.instance.removeTipsDisplay(wineItem.symbolSkin);
				wineItem.destroyView();
			}
			itemList = null;
		}
	}
}